package com.iotek.ssm.util;

import com.iotek.ssm.pojo.*;
import com.iotek.ssm.service.ApplyService;
import com.iotek.ssm.service.SectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.ui.Model;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Component("util")
public class Util {

    //十六进制下数字到字符的映射数组
    private final String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"};

    @Autowired
    private ApplyService applyService;

    @Autowired
    private SectionService sectionService;

    public void setJumpDetails(Model model, String source, String nextPage, String nextUrl, Integer time) {
        JumpDetails jumpDetails = new JumpDetails(source, nextPage, nextUrl, time);
        model.addAttribute("jumpDetails", jumpDetails);
    }

    public List<ApplySheet> getApplyNotLook() {
        ApplySheetQueryVo asq = new ApplySheetQueryVo();
        asq.setLooked(0);
        List<ApplySheet> applySheets = null;
        try {
            applySheets = applyService.findApplyList(asq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return applySheets;
    }

    public List<Position> getPositions(Integer sId) {
        List<Position> positions = null;
        try {
            positions = sectionService.findPositions(sId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return positions;
    }

    //    对密码进行MD5加密
    public String getMD5(String pwd) {
        try {
            // 创建加密对象
            MessageDigest digest = MessageDigest.getInstance("md5");
            // 调用加密对象的方法，加密的动作已经完成
            byte[] bs = digest.digest(pwd.getBytes());
            // 接下来，对加密后的结果，进行优化，按照mysql的优化思路走
            String hexString = "";
            for (byte b : bs) {
                // 第一步，将数据全部转换成正数：
                int c = 255;
                int temp = b & c;
                // 第二步，将所有的数据转换成16进制的形式
                // 注意：转换的时候注意if正数>=0&&<16，那么如果使用Integer.toHexString()，可能会造成缺少位数
                // 因此，需要对temp进行判断
                if (temp < 16 && temp >= 0) {
                    // 手动补上一个“0”
                    hexString = hexString + "0" + Integer.toHexString(temp);
                } else {
                    hexString = hexString + Integer.toHexString(temp);
                }
            }
            return hexString;
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "";
    }

    public String encodeByMD5_1(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        //确定计算方法
        MessageDigest md5 = MessageDigest.getInstance("MD5");
        BASE64Encoder base64en = new BASE64Encoder();
        //加密后的字符串
        String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
        return newstr;
    }

    /**
     * 对字符串进行MD5编码
     */
    private String encodeByMD5_2(String originString) {
        if (originString != null) {
            try {
                //创建具有指定算法名称的信息摘要
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                //使用指定的字节数组对摘要进行最后更新，然后完成摘要计算
                byte[] results = md5.digest(originString.getBytes());
                //将得到的字节数组变成字符串返回
                String result = byteArrayToHexString(results);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 轮换字节数组为十六进制字符串
     *
     * @param b 字节数组
     * @return 十六进制字符串
     */
    private String byteArrayToHexString(byte[] b) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0, n = b.length; i < n; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    //将一个字节转化成十六进制形式的字符串
    private String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n = 256 + n;
        int high = n / 16;
        int low = n % 16;
        return hexDigits[high] + hexDigits[low];
    }

}
